package com.orhon.smartcampus.modules.teacher.mutation;

import java.util.Date;

import com.orhon.smartcampus.modules.material.dao.BuildingsDao;
import com.orhon.smartcampus.modules.material.dao.FloorDao;
import com.orhon.smartcampus.modules.material.dao.RoomDao;
import com.orhon.smartcampus.modules.material.dao.UnitDao;
import com.orhon.smartcampus.modules.material.entity.Buildings;
import com.orhon.smartcampus.modules.material.entity.Floor;
import com.orhon.smartcampus.modules.material.entity.Room;
import com.orhon.smartcampus.modules.material.entity.Unit;
import com.orhon.smartcampus.modules.systemctl.dao.OrgDepartmentsDao;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDepartments;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.entity.TInformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.teacher.dao.OfficeArrangeDao;
import com.orhon.smartcampus.modules.teacher.entity.OfficeArrange;
import com.orhon.smartcampus.modules.teacher.gqlInputModel.GQLTeacherOfficeInput;
import com.orhon.smartcampus.modules.teacher.inputMapper.TeacherOfficeInputMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddTeacherOfficeMutation implements DataFetcher {

    @Autowired
    OfficeArrangeDao officeArrangeDao;
    @Autowired
    BuildingsDao buildingsDao;
    @Autowired
    UnitDao unitDao;
    @Autowired
    FloorDao floorDao;
    @Autowired
    RoomDao roomDao;
    @Autowired
    OrgDepartmentsDao orgDepartmentsDao;
    @Autowired
    TInformationDao  tInformationDao;

    /**
     * 新增/修改教师办公室
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {
        GQLTeacherOfficeInput input = TeacherOfficeInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        OfficeArrange officeArrange = null;

        if (input.getId() != null) {
            officeArrange = officeArrangeDao.get(OfficeArrange.class, input.getId());
            officeArrange.setUpdatedAt(new Date());
        } else {
            officeArrange = new OfficeArrange();
            officeArrange.setCreatedAt(new Date());
        }
        officeArrange.setOfficeName(JSON.toJSONString(input.getName()));
        if(input.getBuildings_id() != null){
            officeArrange.setMaterialBuildingsByBuildingsId(buildingsDao.get(Buildings.class,input.getBuildings_id()));
        }
        if(input.getUnit_id() != null){
            officeArrange.setMaterialUnitByUnitId(unitDao.get(Unit.class,input.getUnit_id()));

        }
        if(input.getFloor_id() != null){

            officeArrange.setMaterialFloorByFloorId(floorDao.get(Floor.class,input.getUnit_id()));
        }
        if(input.getRoom_id() != null){
            officeArrange.setMaterialRoomByRoomId(roomDao.get(Room.class,input.getRoom_id()));
        }
        if(input.getDepartment_id() != null){
            officeArrange.setSystemctlOrgDepartmentsByDepartmentId(orgDepartmentsDao.get(OrgDepartments.class,input.getDepartment_id()));
        }
        if(input.getHead_user() != null){

            officeArrange.setTeacherTInformationByHeadUser(tInformationDao.get(TInformation.class,input.getHead_user()));
        }
//        officeArrange.setBuildingsId(input.getBuildings_id());
//        officeArrange.setUnitId(input.getUnit_id());
//        officeArrange.setFloorId(input.getFloor_id());
//        officeArrange.setRoomId(input.getRoom_id());
//        officeArrange.setDepartmentId(input.getDepartment_id());
//        officeArrange.setHeadUser(input.getHead_user());
        try{
            officeArrangeDao.saveOrUpdate(officeArrange);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

